package com.gtm.service;

import com.gtm.model.Review;
import com.gtm.model.Topic;
import com.gtm.model.User;
import com.gtm.repository.ReviewRepository;
import com.gtm.repository.TopicRepository;
import com.gtm.repository.UserRepository;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ReviewService extends BaseService<Review>{
    @Autowired
    MongoTemplate mt;

    @Autowired
    private ReviewRepository rvr;

    @Autowired
    UserRepository usr;

    @Autowired
    TopicRepository tpr;


    public Review uploadReview(String sid, String summary, String keys, String references, MultipartFile reviewFile) throws IOException {
        //先判断学生是否通过任务书
        //在判断学生是否通过文献，通过后不能上传
        User user = usr.findOne(sid);
        if(user.getAdditionalData()==null||user.getAdditionalData().get("state")==null)
            return null;
        String state = user.getAdditionalData().get("state").toString();
        List<String> stlist=new ArrayList<String>();
        stlist.add("ASSIGNMENT_PASS");stlist.add("REVIEW_PENDING");
        if(stlist.contains(state)==false)
            return null;
        Review rv=null;
        if(rvr.findByStuId(sid)!=null) {
            rv = rvr.findByStuId(sid);
        }
        else {
            rv=new Review();
            rv.setStuId(sid);
        }
        String tid=user.getTopicId();
        String sname=user.getRealname();
        rv.setStuName(sname);
        rv.setTopicId(tid);
        rv.setSummary(summary);
        rv.setKeys(keys);
        rv.setReferences(references);
        String refile=upUserFile(reviewFile,rv);
        DBObject ref=new BasicDBObject();
        ref.put("reviewDoc",refile);
        ref.put("uploadTime",new Date());
        List<DBObject> relist=new ArrayList<DBObject>();
        if(null!= rv.getReviewFile())
            relist= rv.getReviewFile();
        relist.add(ref);
        rv.setReviewFile(relist);
        Topic tpc=tpr.findOne(tid);
        String topicNmae=tpc.getTopicName();
        String teacherName=tpc.getTeacherName();
        String teacherId=tpc.getTeacherId();
        rv.setTopicName(topicNmae);
        rv.setTeacherId(teacherId);
        rv.setTeacherName(teacherName);
        user.setAdditionalData("state","REVIEW_PENDING");
        user.setAdditionalData("reviewId",rvr.save(rv).getId());
        usr.save(user);
        return null;
    }

    public Review passReview(String rid, String comment) {
        Review rev=new Review();
        rev=rvr.findOne(rid);
        rev.setState("PASS");
        rev.setComment(comment);
        String sid=rev.getStuId();
        User user=usr.findOne(sid);
        user.setAdditionalData("state","REVIEW_PASS");
        usr.save(user);
        return rvr.save(rev);
    }


    public Review listByTid(String tid) {
        return rvr.findByTopicId(tid);
    }

    public Review depassReview(String rid, String comment) {
        Review rev=new Review();
        rev=rvr.findOne(rid);
        rev.setState("PENDING");
        rev.setComment(comment);
        String sid=rev.getStuId();
        User user=usr.findOne(sid);
        user.setAdditionalData("state","REVIEW_PENDING");
        usr.save(user);
        return rvr.save(rev);
    }
}
